console.log('=== ?? 空值合并运算符  ===')

// 为变量赋默认值，与 逻辑或|| 的使用对比
let name = ''

let Bob = name || 'Bob'
console.log('||: ', Bob) //=> Bob

let Henry = name ?? 'Henry'
console.log('??: ', Henry) //=> ''

console.log('=== ?. 可选链运算符  ===')

const user = {
  name: 'henry',
  age: 12
}

// console.log(user.weight.size) //=> TypeError ...
console.log(user.weight?.size) //=> undefined

console.log('=== ??= 逻辑空赋值  ===')

let x = ''
let result = (x ??= 'default')
console.log('x: ', result) //=> ''

let y = null
let result2 = (y ??= 'default')
console.log('y: ', result2) //=> 'default'

// 应用，给函数/配置的参数设置默认值
const config = {
  title: 'My App',
  description: 'A sample application',
  version: '0.0.1'
}

config.author ??= '小李大王'
console.log('config', config)

const initConfig = config => {
  config.title ??= 'Default Title'
  config.description ??= 'Default Description'
  config.version ??= '0.0.1'
}

console.log('=== ?= 逻辑空赋值  ===')
